package com.zhipu.balance.ui.vm.home.calculate.rapeseed

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordCorn
import com.zhipu.balance.mobile.event.RecordRapeseed
import com.zhipu.balance.mobile.event.RecordWheat
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 油菜籽未熟粒
 */
class RapeseedImmatureGrainViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 油菜籽不实体
     */
    var mRecordRapeseed : RecordRapeseed? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 未熟粒数 检测可能有两次
     */
    var immatureGrainNumber1 = ObservableField("")


    /**
     * 未熟粒数 检测可能有两次
     */
    var immatureGrainNumber2 = ObservableField("")


    /**
     * 未熟粒-结果
     */
    var immatureGrainResult = ObservableField("")



    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRapeseed?.immatureGrainNumberRecorder)) {
            //未熟粒数 检测可能有两次
            immatureGrainNumber1.set(mRecordRapeseed?.immatureGrainNumber1.toString())
            //未熟粒数 检测可能有两次
            immatureGrainNumber2.set(mRecordRapeseed?.immatureGrainNumber2.toString())
            //未熟粒-结果
            immatureGrainResult.set(mRecordRapeseed?.immatureGrainResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //未熟粒数 检测可能有两次
        immatureGrainNumber1.set("")
        //未熟粒数 检测可能有两次
        immatureGrainNumber2.set("")
        //未熟粒-结果
        immatureGrainResult.set("")
    }






    /**
     * 计算
     */
    fun calculation(){
        if(TextUtils.isEmpty(immatureGrainNumber1.get())){
            immatureGrainNumber1.set("0")
            if(TextUtils.isEmpty(immatureGrainNumber2.get())){
                immatureGrainNumber2.set("0")
            }
        }
        var mThermalDamageNumber1 = immatureGrainNumber1.get()!!.toDouble()
        if (mThermalDamageNumber1 > 0) {
            //
            mRecordRapeseed!!.immatureGrainNumber1 = mThermalDamageNumber1.toInt()
            //热损伤粒数2
            val thermalDamageNumber2 = immatureGrainNumber2.get()!!.toDouble()
            //
            mRecordRapeseed!!.immatureGrainNumber2 = thermalDamageNumber2.toInt()
            //热损伤粒-结果
            var mThermalDamageGrain = CommonUtils.formatDouble(((mThermalDamageNumber1 + thermalDamageNumber2) / 1000) * 100)
            mRecordRapeseed!!.immatureGrainResult = mThermalDamageGrain
            immatureGrainResult.set(mThermalDamageGrain.toString())
        }else{
            //
            mRecordRapeseed!!.immatureGrainNumber1 = mThermalDamageNumber1.toInt()
            //热损伤粒-结果
            val mThermalDamageGrain = (0 / 1000 * 100).toDouble()
            mRecordRapeseed!!.immatureGrainResult = mThermalDamageGrain
            immatureGrainResult.set(mThermalDamageGrain.toString())
        }


        /**
         * 未熟粒数记录员
         */
        mRecordRapeseed!!.immatureGrainNumberRecorder = mPersonnel!!.name
    }
}